package com.nariano.server.domain.engine.droolsimpl;


import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.Assert;

import com.nariano.server.domain.entities.Area;
import com.nariano.server.domain.entities.City;
import com.nariano.server.domain.entities.Population;
import com.nariano.server.domain.entities.PopulationType;
import com.nariano.server.publisher.ClientPublisher;
import com.nariano.server.publisher.events.PopulationChanged;
import com.nariano.server.services.cityservice.CityService;

public final class EvolveCityAction {
    
	@Autowired
	@Qualifier(value="clientPublisher")
    ClientPublisher clientPublisher;
    
    /**
     * Ctr used by test
     * @param cityService
     * @param populationTypeList
     */
    public EvolveCityAction(CityService cityService) {
        this.cityService = cityService;
    }
    
    public EvolveCityAction() {
    }

     
    @Autowired
    @Qualifier("cityService")
    CityService cityService;
    
    private Logger logger = Logger.getLogger(EvolveCityAction.class.getCanonicalName());
    
    /**
     * Adds a selected popluation, saved 
     * @param city
     */
    public void addSelectedPopulation(Area area) {
    	Assert.notNull(area.getCity());
		City city = area.getCity();
		PopulationType type = city .getCreateType();
		Assert.notNull(type);
		Population p = new Population(city.getOwnerId(), type, 1);
		city.addPopulation(p);

		if (logger.isLoggable(Level.FINE)) {
			logger.fine("Adds a " + p.type().getName() + " to "
					+ city.getName() + " at " + city.getLocation() + " population: " + city.getPopulationCount());
		}

		cityService.updateCity(city);
		
		clientPublisher.publish(new PopulationChanged(area, p.getUserId(), p.type(), p.getNumber()));
	}
  
}
